<?php

namespace App;


//use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;

class AdminUser extends Authenticatable
{
   // use Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
//    protected $fillable = [
//        'name', 'password','address','email','phone'
//    ];
    protected $guarded=[];
//用户有哪一些角色
    public function roles(){
        return $this->belongsToMany(\App\AdminRole::class,'admin_role_user','user_id','role_id')->withPivot(['user_id','role_id']);
    }
    //判断是否有某个角色
    public function isInRoles($roles){
        return !!$roles->intersect($this->roles)->count();
    }

    //给用户分配角色
    public function assignRole($role){
        return $this->roles()->save($role);
    }

    //取消用户分配的角色
    public function deleteRole($role){
        return $this->roles()->detach($role);
    }

    //用户是否有权限

    public function hasPermission($permission){
        return $this->isInRoles($permission->roles);
    }
    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */

    public  function notices(){

        return $this->belongsToMany(\App\Notice::class,'user_notice','user_id','notice_id')->withPivot(['user_id','notice_id']);
    }
    //增加通知
    public function AddNotices($notice){
        return $this->notices()->save($notice);
    }
    //删除通知
    public function DelNotices($notice){
        return $this->notices()->detach($notice);
    }
}
